package com.fast.thread;


import cn.hippo4j.core.executor.DynamicThreadPool;
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
import com.fast.config.JsonConvert;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ConversionServiceFactoryBean;
import org.springframework.core.annotation.Order;
import org.springframework.core.convert.ConversionService;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ThreadPoolExecutor;

@Order(0)
@Configuration
public class ThreadPoolConfig {
    @Bean("conversionService")
    public ConversionService conversionService() {
        ConversionServiceFactoryBean factoryBean = new ConversionServiceFactoryBean();
        Set<Object> tmp = new HashSet<>();
        tmp.add(new JsonConvert());
        factoryBean.setConverters(tmp);
        factoryBean.afterPropertiesSet();
        return factoryBean.getObject();
    }

    private final static String produceThreadPoolId = "wz-produce";
    private final static String consumerThreadPoolId = "wz-consume";

    @DynamicThreadPool
    @Bean(consumerThreadPoolId)
    public ThreadPoolExecutor messageConsumeDynamicExecutor() {
        ThreadPoolExecutor executor = ThreadPoolBuilder.builder()
                .threadFactory(consumerThreadPoolId)
                .threadPoolId(consumerThreadPoolId)
                .dynamicPool()
                .build();
       return executor;
    }


    @DynamicThreadPool
    @Bean(produceThreadPoolId)
    public ThreadPoolExecutor messageProduceDynamicExecutor() {
        ThreadPoolExecutor messageProduceDynamicExecutor = ThreadPoolBuilder.builder()
                .threadFactory(produceThreadPoolId)
                .threadPoolId(produceThreadPoolId)
                .dynamicPool()
                .build();
        return messageProduceDynamicExecutor;
    }


//    @DynamicThreadPool
//    @Bean(consumerThreadPoolId)
//    public ThreadPoolTaskExecutor consumerThreadPool() {
//        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
//        threadPoolTaskExecutor.setBeanName(consumerThreadPoolId);
//        threadPoolTaskExecutor.setThreadNamePrefix(consumerThreadPoolId);
//        return threadPoolTaskExecutor;
//    }
//
//    @DynamicThreadPool
//    @Bean(produceThreadPoolId)
//    public ThreadPoolTaskExecutor produceThreadPool() {
//        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
//        threadPoolTaskExecutor.setBeanName(produceThreadPoolId);
//        threadPoolTaskExecutor.setThreadNamePrefix(produceThreadPoolId);
//        return threadPoolTaskExecutor;
//    }


}
